import { formatChartsDataFn } from '../../utils/commonFunc';
import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'portfolioOptimizationM',
  state: {
    resultDataObj: {
      config: {
        columns: [],
        dataSource: [],
      },
      performComp: {
        columns: [],
        dataSource: [],
      },
      exposureComp: {},
    }, //结果页数据
    importData: {}, //导入的数据
    isShowTitle: true, //是否展示组合优化标题
  },
  effects: {
    //获取token值
    *saveToken({ payload }, { call, put }) {
      const data = yield call(api.portfolioOptimization1.saveToken, payload);
      if (data && data.success) {
        return data;
      } else {
        message.error('导出失败');
      }
    },
    //模糊查询资产下拉列表
    *queryAsset({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.queryAsset, payload);
      callback(response);
    },
    //执行分析按钮 请求分析数据
    *executeOptimization({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.executeOptimization, payload);
      if (response) {
        if (response.code == 0) {
          callback('success');
          yield put({
            type: 'setState',
            payload: {
              resultDataObj: { ...response.data },
            },
          });
        } else {
          callback('fail');
          message.error(response.msg || '提交失败!');
        }
      } else {
        callback('fail');
        message.error('提交失败!');
      }
    },
    //上传文件
    *uploadFile({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.uploadFile, payload);
      callback(response);
    },
    //获取示例组合列表
    *getSamplePortList({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.getSamplePortList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取示例组合列表失败!');
        }
      } else {
        callback('fail');
        message.error('获取示例组合列表失败!');
      }
    },
    //获取示例组合对应的表格数据
    *getSamplePortTable({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.getSamplePortTable, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '示例组合表格查询失败!');
        }
      } else {
        callback('fail');
        message.error('示例组合表格查询失败!');
      }
    },
    //获取累计收益率
    *getChartsYield({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'getChartsYield1';
      } else {
        url = 'getChartsYield';
      }
      let fromBarra = payload.fromBarra;
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response) {
        if (response.code == 0) {
          //因barra组合优化的图表返回结构变更，故这里做一下处理
          if (fromBarra) {
            response.data = formatChartsDataFn(response.data, 'line');
          }
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取累计收益率失败!');
        }
      } else {
        callback('fail');
        message.error('获取累计收益率失败!');
      }
    },
    //获取获取回撤
    *getCharts({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'getCharts1';
      } else {
        url = 'getCharts';
      }
      let fromBarra = payload.fromBarra;
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response) {
        if (response.code == 0) {
          //因barra组合优化的图表返回结构变更，故这里做一下处理
          if (fromBarra) {
            response.data = formatChartsDataFn(response.data, 'line');
          }
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取回撤失败!');
        }
      } else {
        callback('fail');
        message.error('获取回撤失败!');
      }
    },
    //获取逐月年化收益率
    *getChartsYearYield({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'getChartsYearYield1';
      } else {
        url = 'getChartsYearYield';
      }
      let fromBarra = payload.fromBarra;
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response) {
        if (response.code == 0) {
          //因barra组合优化的图表返回结构变更，故这里做一下处理
          if (fromBarra) {
            response.data = formatChartsDataFn(response.data, 'bar');
          }
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取逐月年化收益率失败!');
        }
      } else {
        callback('fail');
        message.error('获取逐月年化收益率失败!');
      }
    },
    //组合变更查询 业绩比较基准默认值
    *queryBenchmarkId({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.queryBenchmarkId, payload);
      if (response && response.code == 0) {
        callback(response.data?.dataSource || {});
      } else {
        callback();
        message.error(response?.msg || '查询业绩比较基准默认值失败!');
      }
    },
    //获取业绩比较基准下拉列表 暂时没有用
    *queryBenchmarkList({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.queryBenchmarkList, payload);
      if (response && response.code == 0) {
        callback(response.data?.dataSource || []);
      } else {
        callback();
        message.error(response?.msg || '查询业绩比较基准下拉失败!');
      }
    },
    //查询风险因子约束表格
    *queryRisk({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.queryRisk, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response?.msg || '查询风险因子约束失败!');
      }
    },
    //执行分析按钮 请求分析数据 股票
    *executeOptimization1({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioOptimization1.executeOptimization, payload);
      if (response && response.code == 0) {
        callback(response.data || '1');
      } else {
        message.error(response?.msg || '提交失败!');
      }
    },
    //根据id查询参数设置的回填数据
    *querySettingById({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'querySettingById1';
      } else {
        url = 'querySettingById';
      }
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response && response.code == 0) {
        callback(response.data || {});
      } else {
        callback();
        message.error(response?.msg || '查询参数详情失败!');
      }
    },
    //查询组合优化分析别表
    *queryMyTableList({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'queryMyTableList1';
      } else {
        url = 'queryMyTableList';
      }
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response?.msg || '查询组合优化分析别表失败!');
      }
    },
    //查询优化详情byId
    *queryResultDetail({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'queryResultDetail1';
      } else {
        url = 'queryResultDetail';
      }
      let fromBarra = payload.fromBarra;
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response && response.code == 0) {
        //因barra组合优化的结果列表变更，故这里做一下处理
        if (fromBarra) {
          let data = response.data || {};
          data.config = { dataSource: data.config };
          data.exposureComp = formatChartsDataFn(data.exposureComp, 'bar');
          data.performComp = { dataSource: data.performComp };
          response.data = data;
        }
        callback(response.data);
      } else {
        callback();
        message.error(response?.msg || '查询分析结果详情失败!');
      }
    },
    //删除
    *deleteData({ payload, callback }, { call, put }) {
      let url = '';
      if (payload.fromBarra) {
        url = 'deleteData1';
      } else {
        url = 'deleteData';
      }
      delete payload.fromBarra;
      const response = yield call(api.portfolioOptimization1[url], payload);
      if (response && response.code == 0) {
        callback();
        message.success('删除成功!');
      } else {
        message.error(response?.msg || '删除失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
